#include <stdio.h>
#include <stdlib.h>
#include <csp_spu.h>
#include <csp_debug.h>
#include "../fh_test.h"




int global_pid; 


int Run(){
	int pid = csp_pid();

	global_pid = csp_pid();

	printf("Farmer starting with pid %i.\n", pid);

	int i __attribute__ ((aligned(16)));
	int interval[2] __attribute__ ((aligned(16)));

	csp_channel init2farmer;
	csp_channel farmer2workers;

	init2farmer    = csp_create_channel(ONE2ONE_CHANNEL, INIT2FARMER, sizeof(int)*2);
	farmer2workers = csp_create_channel(ONE2ANY_CHANNEL, FARMER2WORKERS, sizeof(int));

	csp_channel_read(init2farmer, interval);


	for(i=interval[0]; i < interval[1]; i++){
		csp_channel_write(farmer2workers, &i);
	}

	csp_poison_channel(farmer2workers);

	ASSERT( pid == csp_pid() );
	ASSERT( global_pid == csp_pid() );

	PRINTF_FOR_TESTING("%s terminating on spe #%i with pid %i\n",__FILE__ , csp_spe_id(), pid);

	PRINTF_FOR_TESTING("Nested task with pid=%i terminated with res=%i.\n", pid, res);

	return 0;
}
